<?php
/**
 * example of post and file_get_contents
 * example of image creation
 */
// $filename = "lzaElaStat.png";
// $patternFilename = 'template50px.png';
// $gcUserGuid = 'e047ed04-7b51-46c8-b9ab-0c638eb4ac77'; 
// $ocUserId = 9067;

$patternFilename = $_GET[1].'.png';
$gcUserGuid = $_GET[2]; 
$ocUserId = $_GET[3];
$filename = $_GET[3].'-'.$_GET[2].'.png';


date_default_timezone_set('Europe/Warsaw');
if(!file_exists($patternFilename)) {
	print "exit - no pattern file";
	exit;
}
if (date('Ymd', filemtime($filename)) != date('Ymd')) {
	if (isset($_GET[4])) $cacheName = $_GET[4]; else $cacheName = 'keszynek';
	$pattern = imagecreatefrompng($patternFilename);
	$image = imagefillfromfile($pattern, 450, 50); 
	$textColor = imagecolorallocate($image, 0, 0, 10);
	$gc = getGcFounds($gcUserGuid);
	$oc = getOcFounds($ocUserId);
	$occf = $oc->caches_found;
	$own = $oc->caches_hidden;
	//$reko =$oc->rcmds_given;
	$reko = getReco($_GET[3]);
	$tot = $gc+$occf;
	imagestring($image, 2, 85, 3,  "znalezione $gc skrytek i $occf $cacheName.", $textColor);
	imagestring($image, 2, 85, 15, "razem: $tot", $textColor);
	imagestring($image, 2, 85, 32,  "posadzone $own $cacheName", $textColor);
	imagestring($image, 2, 270, 32, 'x '.$reko, $textColor);
	imagepng($image, $filename);
	imagedestroy($image);
	imagedestroy($pattern);
}
header('Content-Type: image/png');
echo file_get_contents($filename);

function getGcFounds($UserGUID) {
	$postdata = http_build_query(array("Language" => '', "CacherName" => '', "UserGUID" => $UserGUID, ".submit" => 'Submit', ));
	$opts = array(
		'http' => array(
			'method' => 'POST', 
			'header' => 
			'Content-type: application/x-www-form-urlencoded', 
			'content' => $postdata,
			'timeout' => 5,
		)
	);
	$context = stream_context_create($opts);
	$result = file_get_contents('http://www.cacherstats.com//cgi-bin/GeocacherLocations.pl?Language=', false, $context);
	if ($result) {
		$res1 = explode('Total Finds: ', $result);
		$res2 = explode('</FONT>', $res1[1]);
		return $res2[0];
	} else {
		return 0;
	}
}

function getOcFounds($ocuserId){
	$userUuid = '';
	$consumer_key = "gNbYGdbEfRKSPnjxY46K";
	$json = file_get_contents(
		"http://opencaching.pl/okapi/services/users/by_internal_id".
		"?internal_id=".$ocuserId.
		"&consumer_key=".$consumer_key.
		"&fields=caches_found|caches_hidden|rcmds_given 
		"
	);
	
	$data = json_decode($json);
	return $data;
}

function imagefillfromfile($image, $width, $height) {
	$imageWidth = imagesx($image);
	$imageHeight = imagesy($image);
	$newImage = imagecreatetruecolor($width, $height);
	
	for ($imageX = 0; $imageX < $width; $imageX += $imageWidth) {
	    for ($imageY = 0; $imageY < $height; $imageY += $imageHeight) {
	        imagecopy($newImage, $image, $imageX, $imageY, 0, 0, $imageWidth, $imageHeight);
	    }
	}
	return($newImage);
	imagedestroy($newImage);
}

function getReco($ocuserId) {
	$url = 'http://opencaching.pl/powerTrail/ajaxGetTopRatings.php?u='.$ocuserId;
	return file_get_contents($url);
}
